<template>
	<view class="container">
		<view class="content">
			<view class="list" v-for="(item,index) in addressList" :key="index" @click="selectClick(index)">
				<!-- <view class="select" v-if="isType !== 'default'">
					<u-icon name="checkmark-circle-fill" color="#EA3D37" size="40" v-if="item.select == true"></u-icon>
					<view class="no" v-else></view>
				</view> -->
				<view class="infos">
					<view class="user_info">{{item.real_name+' '+item.phone}}
						<view v-if="item.is_default == 1">默认</view>
					</view>
					<view class="address">{{item.detail}}</view>
				</view>
				<view style="height: 80rpx;width: 80rpx;display: flex;align-items: center;justify-content: center;"
					@click.stop="$gourl('/mallPage/address/editAddress?info='+JSON.stringify(item))">
					<u-icon name="edit-pen" color="#999" size="40"></u-icon>
				</view>
			</view>
		</view>
		<view class="add_btn" @click="$gourl('/mallPage/address/addAddress')">
			<!-- <u-icon name="plus-circle" color="#EA3D37" size="40"></u-icon> -->
			<text style="margin-left: 20rpx;">新增地址</text>
		</view>
		<u-loadmore :status="status" v-if="status !== 'nomore'" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				selectId: null,
				isType: 'default',
				page: 1,
				pageSize: 10,
				total: 1,
				count: 1,
				status: 'loadmore'
			}
		},
		onShow() {
			if (uni.getStorageSync('selectAddressId')) {
				this.selectId = uni.getStorageSync('selectAddressId')
			}
			this.status = 'loadmore'
			this.addressList = []
			this.getData()
		},
		onLoad(opdata) {
			if (opdata.isType) {
				this.isType = opdata.isType
			}
		},
		onReachBottom() {
			if (this.page >= this.count) return this.status = 'nomore';
			this.status = 'loading';
			this.page = ++this.page;
			this.getData()
		},
		methods: {
			async getData() {
				await this.$allApi.addressList({
					page: this.page,
					limit: this.pageSize
				}).then(res => {
					if (res.data.length < this.pageSize) {
						this.status = 'nomore'
					} else {
						this.status = 'loadmore'
					}
					this.addressList = [...res.data, ...this.addressList]
				})
			},
			selectClick(index) {
				if (this.isType == 'default') {
					return
				}
				for (let i = 0; i < this.addressList.length; i++) {
					if (i == index) {
						this.addressList[i].select = true
						uni.setStorageSync('selectAddressId', this.addressList[i].address_id)
						getApp().selectAddress = JSON.stringify(this.addressList[i])
					} else {
						this.addressList[i].select = false
					}
				}
				this.$forceUpdate()
				this.$backTo(1)
			}
		}
	}
</script>

<style scoped lang="scss">
	.no {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		border: 1px solid #ccc;
	}

	.container {
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: 120rpx;
		background: #f8f8f8;
	}

	.list {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx;
		margin-bottom: 20rpx;
		border-radius: 10rpx;
		background: #fff;

		.infos {
			flex: 1;
			padding: 0 20rpx;
			line-height: 2;

			.user_info {
				font-size: 30rpx;
				font-weight: 400;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				view {
					width: 74rpx;
					height: 30rpx;
					border-radius: 6rpx;
					margin-left: 20rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 18rpx;
					color: #EA3D37;
					background: rgba(234, 61, 55, 0.08);

				}
			}

			.address {
				font-size: 24rpx;
			}
		}
	}

	.add_btn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100vw;
		height: 70rpx;
		border-radius: 35rpx;
		background: #EA3D37;
		color: #fff;

		bottom: calc(20rpx + constant(safe-area-inset-bottom));
		bottom: calc(20rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: center;
		border-top: 1px solid #eee;
	}
</style>